package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-20
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper,NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private INursingProjectPlanService iNursingProjectPlanService;
    @Autowired
    private RedisTemplate<Object , Object> redisTemplate;

    private static final String CACHE_KEY_PREFIX = "nursingplan:all";

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id)
    {

        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        //1.查询护理计划表数据，然后进行封装数据
        NursingPlan nursingPlan = getById(id);
        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);

        //2.根据护理计划id去查询中间表数据集合，然后进行数据封装
        List<NursingProjectPlan> nursingProjectPlans = iNursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        //3.类型转换
        List<NursingProjectPlanVo> nursingProjectPlanVos = nursingProjectPlans.stream().map(nursingProjectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
            nursingProjectPlanVo.setProjectId(String.valueOf(nursingProjectPlan.getProjectId()));
            nursingProjectPlanVo.setExecuteCycle(nursingProjectPlan.getExecuteCycle()+"");
            nursingProjectPlanVo.setId(Long.valueOf(nursingProjectPlan.getId()));
            nursingProjectPlanVo.setExecuteTime(nursingProjectPlan.getExecuteTime());
            nursingProjectPlanVo.setExecuteFrequency(Long.valueOf(nursingProjectPlan.getExecuteFrequency()));
            nursingProjectPlanVo.setPlanId(Long.valueOf(nursingProjectPlan.getPlanId()));
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());

        nursingPlanVo.setProjectPlans(nursingProjectPlanVos);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //1.新增护理计划表数据
        NursingPlan nursingPlan=new NursingPlan();
        /**
         *  利用bean对象拷贝技术实现
         *  要求，两个对象中的属性名称以及类型必须完全一致，才可以拷贝成功
         */
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);

        this.save(nursingPlan);
        //2.批量新增中间表 数据
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (!CollectionUtils.isEmpty(projectPlans)){
            projectPlans.forEach(projectPlan -> {
                projectPlan.setPlanId(nursingPlan.getId());
            });
        }
        boolean flag = iNursingProjectPlanService.saveBatch(projectPlans);
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return flag?1:0;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //1.判断护理项目列表是否为空
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (!CollectionUtils.isEmpty(projectPlans)){
            //批量删除中间表数据
            iNursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlanDto.getId()));
            //批量新增中间表数据
            projectPlans.forEach(projectPlan -> {
                projectPlan.setPlanId(Long.valueOf(nursingPlanDto.getId()+""));
            });
            iNursingProjectPlanService.saveBatch(projectPlans);
        }

        //如果为空，直接修改护理计划表
        NursingPlan nursingPlan=new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        boolean flag = updateById(nursingPlan);
        return flag?1:0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNursingPlanById(Integer id)
    {
        //1.批量删除中间表数据
        iNursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        //2.批量删除护理计划表数据
        boolean flag = removeById(id);
        return flag?1:0;
    }

    @Override
    public List<NursingPlan> getAll() {
        List<NursingPlan> list=nursingPlanMapper.getAll();
        return list;
    }
}
